<?php

namespace App\Console\Commands\Check;

use App\Enums\Check\CheckEnum;
use App\Enums\Pay\RmsPayEnum;
use App\Models\Check\CheckStreamModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderPayHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSpaceDesignCostMappingModel;
use App\Service\Check\CheckStreamParam;
use App\Service\Check\CheckStreamService;
use App\Service\OpenShop\Feishu\SpaceDesignService;
use Illuminate\Console\Command;

class AddOpenShopPay extends Command
{

    protected $signature = 'check:add-open-shop-pay';

    protected $description = '开店支付补录';

    public function handle()
    {
        // 初始化变量 - 不用动
        $feeOrderNo = '';
        $preparationNo = '';
        $designFee     = 0;

        // ****** 需要自己输入的start ******
        $orderNo  = 'KD2024033063015579';
        $payPrice = 19600; // 单位元
        $payDate  = '2024-03-30';
        $sysType  = CheckEnum::SYS_TYPE_OPEN_SHOP_LEAGUE;
        $payChannel = OpenShopSignOrderPayHistoryModel::PAY_CHANNEL_WX;

        // 年费
//        $feeOrderNo = 'NF2022092507456326179';

        // 设计费
//        $preparationNo = 'CB2024032733723297'; // 不传会insert
//        $designFee = 3000; // 设计费，单位元

        // ****** 需要自己输入的end ******

        $payPrice *= 100;
        $designFee *= 100;

        // 加盟商姓名
        $signOrder = OpenShopSignOrderModel::query()->where('orderNo', $orderNo)->first();
        if (!$signOrder) {
            dd('不存在的订单号');
        }

        $userName = (string)optional($signOrder->identityCardRel)->name;

        switch ($sysType) {
            case CheckEnum::SYS_TYPE_OPEN_SHOP_LEAGUE:
                $bizId   = $orderNo;
                $subject = $userName . '-' . RmsPayEnum::SYS_TYPE_TEXT[$sysType];
                break;
            case CheckEnum::SYS_TYPE_OPEN_SHOP_DESIGN:

                if (!$preparationNo) {

                    // space_design_cost
                    OpenShopSpaceDesignCostMappingModel::query()->insert([
                        'orderNo'       => $orderNo,
                        'preparationNo' => $bizId = SpaceDesignService::genPreparationNo("CB"),
                        'designFee'     => $designFee,
                        'totalFee'      => $designFee,
                        'payFee'        => $designFee,
                        'orderStatus'   => OpenShopSpaceDesignCostMappingModel::ORDER_STATUS_1,
                        'payStatus'     => OpenShopSpaceDesignCostMappingModel::ORDER_PAY_STATUS_2,
                        'payTime'       => strtotime($payDate),
                        'createdAt'     => strtotime($payDate),
                        'updatedAt'     => strtotime($payDate),
                    ]);
                } else {
                    $bizId = $preparationNo;
                }

                $subject = $userName . '-' . RmsPayEnum::SYS_TYPE_TEXT[$sysType];
                break;
            case CheckEnum::SYS_TYPE_ANNUAL_FEE:
                $bizId = $feeOrderNo;
                $subject = $userName . '-' . RmsPayEnum::SYS_TYPE_TEXT[$sysType];
                break;

            default:
                dd('未配置sysType case');
        }

        $subject .= "（手动导入）";

        // sign_order_pay_history
        OpenShopSignOrderPayHistoryModel::query()->insert([
            'orderNo'        => $orderNo,
            'payNo'          => '',
            'shouldPayPrice' => $payPrice,
            'payPrice'       => $payPrice,
            'payStatus'      => OpenShopSignOrderPayHistoryModel::PAY_STATUS_OK,
            'payChannel'     => $payChannel,
            'payTime'        => strtotime($payDate),
            'type'           => OpenShopSignOrderPayHistoryModel::TYPE_FINAL,
            'sysType'        => $sysType,
            'bizId'          => $bizId,
            'subject'        => $subject,
            'createdAt'      => strtotime($payDate),
            'updatedAt'      => strtotime($payDate),
        ]);

        // check_streams
        $lastSysTypeCheckStream = CheckStreamModel::query()
                                                  ->where('sysType', $sysType)
                                                  ->orderByDesc('id')
                                                  ->first();
        CheckStreamModel::query()->insert([
            'tradeTime'       => strtotime($payDate),
            'date'            => date('Ymd', strtotime($payDate)),
            'orderId'         => $bizId,
            'subject'         => $subject,
            'sysType'         => $sysType,
            'orderPrice'      => $payPrice,
            'tradePrice'      => $payPrice,
            'incomePrice'     => $payPrice,
            'feePrice'        => 0,
            'feeRate'         => 0,
            'thirdPayNo'      => '',
            'company'         => $lastSysTypeCheckStream->company,
            'companyAccount'  => $lastSysTypeCheckStream->companyAccount,
            'payNo'           => '',
            'payChannel'      => $payChannel,
            'payChannelChild' => 0,
            'payAction'       => 0,
            'checkStatus'     => 0,
            'createdAt'       => strtotime($payDate),
            'updatedAt'       => strtotime($payDate),
        ]);
    }
}
